package com.dyna.patterns.design.behavioral.chain;

import lombok.Data;

import java.util.List;

@Data
public abstract class Approver {
    protected Approver nextApprover;
    protected String name;
    protected double approvalLimit;

    public static Approver buildChain(List<Approver> approvers) {
        if (approvers == null || approvers.isEmpty()) {
            throw new IllegalArgumentException("处理者列表不能为空");
        }

        // 按顺序链接所有处理者
        for (int i = 0; i < approvers.size() - 1; i++) {
            Approver current = approvers.get(i);
            Approver next = approvers.get(i + 1);
            current.setNext(next);
        }

        return approvers.get(0); // 返回链头
    }

    public Approver(String name, double limit) {
        this.name = name;
        this.approvalLimit = limit;
    }

    public void setNext(Approver next) {
        this.nextApprover = next;
    }

    public void processRequest(ExpenseRequest request) {
        if (request.getAmount() <= approvalLimit) {
            System.out.println(name + "审批了" + request.getAmount() + "元");
        }else {
            if (nextApprover != null) {
                nextApprover.processRequest(request);
            }else {
                System.out.println("金额过大，需要会议讨论");
            }
        }
    }
}

